/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.actions.login;

import com.mapping.Empleado;
import com.mapping.Login;
import com.mapping.TipoLogin;
import com.opensymphony.xwork2.ActionSupport;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;

/**
 *
 * @author Ricardo
 */
@Namespace(value = "/")
@Action(value = "altaLogin", results = {
@Result(name = "success", location = "/Login/resultadoAltaLogin.jsp")})
public class AltaLoginAction extends ActionSupport{
    
    private int tipo;
    private String user;
    private String pass;
    private String idEmpleado;
    
    @Override
    public String execute(){
        Login login = new Login();
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("inventarios");
        EntityManager em = emf.createEntityManager();
        EntityTransaction tx = em.getTransaction();
        tx.begin();
        //login.setIdLogin(Integer.SIZE);        
        TipoLogin ti = (TipoLogin)em.getReference(TipoLogin.class,tipo);
        login.setIdTipo(ti);
        login.setUser(user);
        login.setPass(pass);
        //Empleado empleado = new Empleado();
        //empleado.setIdEmpleado(idEmpleado);
        Empleado empleado = (Empleado)em.getReference(Empleado.class, idEmpleado);
        login.setIdempleado(empleado);       
        em.persist(login);        
        em.flush();
        tx.commit();
        em.close();        
        return SUCCESS;    
    }

    public int getTipo() {
        return tipo;
    }

    public void setTipo(int tipo) {
        this.tipo = tipo;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getPass() {
        return pass;
    }

    public void setPass(String pass) {
        this.pass = pass;
    }

    public String getIdEmpleado() {
        return idEmpleado;
    }

    public void setIdEmpleado(String idEmpleado) {
        this.idEmpleado = idEmpleado;
    }
        
}
